package com.redoor.app.stake.basicdata.service.impl;

import com.redoor.app.stake.basicdata.dao.BaseStakeOrgIndexMapper;
import com.redoor.app.stake.basicdata.model.BaseStakeOrgIndex;
import com.redoor.app.stake.basicdata.model.constants.BasicDataRedisConstants;
import com.redoor.app.stake.basicdata.service.BaseStakeOrgIndexService;
import com.redoor.app.stake.common.utils.UUIDTool;
import com.redoornetwork.framework.data.redis.util.RedisUtils;
import com.redoornetwork.framework.mybatis.service.AbstractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by wangjunlei on 2018-03-13 17:22:44.
 */
@Service
public class BaseStakeOrgIndexServiceImpl extends AbstractService<BaseStakeOrgIndex, String> implements BaseStakeOrgIndexService {

    @Autowired
    private BaseStakeOrgIndexMapper baseStakeOrgIndexMapper;

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public Double getStakeIndexByOrgNo(String orgNo) {
        if (StringUtils.isEmpty(orgNo)){
            return 0d;
        }
        if(!redisUtils.hHasKey(BasicDataRedisConstants.REDIS_STAKE_ORG_PRE,orgNo)){
            BaseStakeOrgIndex stakeIndexByOrgNo = baseStakeOrgIndexMapper.getStakeIndexByOrgNo(orgNo);
            if(stakeIndexByOrgNo == null){
                redisUtils.hincr(BasicDataRedisConstants.REDIS_STAKE_ORG_PRE,orgNo,0);
            } else {
                redisUtils.hincr(BasicDataRedisConstants.REDIS_STAKE_ORG_PRE,orgNo,stakeIndexByOrgNo.getNoindex().intValue());
            }
        }
        return redisUtils.hincr(BasicDataRedisConstants.REDIS_STAKE_ORG_PRE,orgNo,1);
    }

    @Override
    public BaseStakeOrgIndex findByOrgNo(String orgno) {
        if (StringUtils.isEmpty(orgno)){
            return null;
        }
        return baseStakeOrgIndexMapper.getStakeIndexByOrgNo(orgno);
    }

    @Override
    public int save(BaseStakeOrgIndex stakeIndexByOrgNo) {
        stakeIndexByOrgNo.setUuid(UUIDTool.getUUID());
        stakeIndexByOrgNo.setCreatedAt(new Date());
        stakeIndexByOrgNo.setUpdatedat(new Date());

        return baseStakeOrgIndexMapper.insert(stakeIndexByOrgNo);
    }
}
